{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Two Loop FDEM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geoscilabs.base import widgetify\n",
    "import geoscilabs.em.InductionLoop as IND\n",
    "from ipywidgets import interact, FloatSlider, FloatText"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameter Descriptions\n",
    "\n",
    "<img style=\"float: right; width: 500px\" src=\"https://github.com/geoscixyz/geosci-labs/blob/master/images/em/InductionLoop.png?raw=true\">\n",
    "\n",
    "Below are the adjustable parameters for widgets within this notebook:\n",
    "\n",
    "* $I_p$: Transmitter current amplitude [A]\n",
    "* $a_{Tx}$: Transmitter loop radius [m]\n",
    "* $a_{Rx}$: Receiver loop radius [m]\n",
    "* $x_{Rx}$: Receiver x position [m]\n",
    "* $z_{Rx}$: Receiver z position [m]\n",
    "* $\\theta$: Receiver normal vector relative to vertical [degrees]\n",
    "* $R$: Resistance of receiver loop [$\\Omega$]\n",
    "* $L$: Inductance of receiver loop [H]\n",
    "* $f$: Specific frequency [Hz]\n",
    "* $t$: Specific time [s]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Background Theory: Induced Currents due to a Harmonic Primary Signal\n",
    "\n",
    "Consider the case in the image above, where a circular loop of wire ($Tx$) caries a harmonic current $I_p (\\omega)$. According to the Biot-Savart law, this produces a harmonic primary magnetic field. The harmonic nature of the corresponding magnetic flux which passes through the receiver coil ($Rx$) generates an induced secondary current $I_s (\\omega)$, which depends on the coil's resistance ($R$) and inductance ($L$). Here, we will provided final analytic results associated with the app below. Full derivations can be found at the bottom of the page.\n",
    "\n",
    "### Frequency Response\n",
    "\n",
    "The frequency response which characterizes the induced currents in $Rx$ is given by:\n",
    "\n",
    "\\begin{equation}\n",
    "I_s (\\omega) = - \\frac{i \\omega  A \\beta_n}{R + i \\omega L} I_p(\\omega)\n",
    "\\end{equation}\n",
    "\n",
    "where $A$ is the area of $Rx$ and $\\beta$ contains the geometric information pertaining to the problem. The induced current has both in-phase and quadrature components. These are given by:\n",
    "\n",
    "\\begin{align}\n",
    "I_{Re} (\\omega) &= - \\frac{i \\omega A \\beta_n R}{R^2 + (\\omega L)^2} I_p(\\omega) \\\\\n",
    "I_{Im} (\\omega) &= - \\frac{ \\omega^2 A \\beta_n L}{R^2 + (\\omega L)^2} I_p(\\omega)\n",
    "\\end{align}\n",
    "\n",
    "### Time-Harmonic Response\n",
    "\n",
    "In the time domain, let us consider a time-harmonic primary current of the form $I_p(t) = I_0 \\textrm{cos}(\\omega t)$. In this case, the induced currents within $Rx$ are given by:\n",
    "\n",
    "\\begin{equation}\n",
    "I_s (t) = - \\Bigg [ \\frac{\\omega I_0 A \\beta_n}{R \\, \\textrm{sin} \\phi + \\omega L \\, \\textrm{cos} \\phi} \\Bigg ] \\, \\textrm{cos} (\\omega t -\\phi) \\;\\;\\;\\;\\; \\textrm{where} \\;\\;\\;\\;\\; \\phi = \\textrm{tan}^{-1} \\Bigg ( \\frac{\\omega L}{R} \\Bigg ) \\, \\in \\, [\\pi/2, \\pi ]\n",
    "\\end{equation}\n",
    "\n",
    "The phase-lag between the primary and secondary currents is represented by $\\phi$. As a result, there are both in-phase and quadrature components of the induced current, which are given by:\n",
    "\\begin{align}\n",
    "\\textrm{In phase:} \\, I_s (t) &= - \\Bigg [ \\frac{\\omega I_0 A \\beta_n}{R \\, \\textrm{sin} \\phi + \\omega L \\, \\textrm{cos} \\phi} \\Bigg ] \\textrm{cos} \\phi \\, \\textrm{cos} (\\omega t) \\\\\n",
    "\\textrm{Quadrature:} \\, I_s (t) &= - \\Bigg [ \\frac{\\omega I_0 A \\beta_n}{R \\, \\textrm{sin} \\phi + \\omega L \\, \\textrm{cos} \\phi} \\Bigg ] \\textrm{sin} \\phi \\, \\textrm{sin} (\\omega t)\n",
    "\\end{align} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d8509878db1e43329cd7ba830e199360",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(FloatSlider(value=1.0, continuous_update=False, description='$I_0$', max=10.0, min=1.0, step=1…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# RUN FREQUENCY DOMAIN WIDGET\n",
    "widgetify(IND.fcn_FDEM_Widget,I=FloatSlider(min=1, max=10., value=1., step=1., continuous_update=False, description = \"$I_0$\"),\\\n",
    "                        a1=FloatSlider(min=1., max=20., value=10., step=1., continuous_update=False, description = \"$a_{Tx}$\"),\\\n",
    "                        a2=FloatSlider(min=1., max=20.,value=5.,step=1.,continuous_update=False,description = \"$a_{Rx}$\"),\\\n",
    "                        xRx=FloatSlider(min=-15., max=15., value=0., step=1., continuous_update=False, description = \"$x_{Rx}$\"),\\\n",
    "                        zRx=FloatSlider(min=-15., max=15., value=-8., step=1., continuous_update=False, description = \"$z_{Rx}$\"),\\\n",
    "                        azm=FloatSlider(min=-90., max=90., value=0., step=10., continuous_update=False, description = \"$\\\\theta$\"),\\\n",
    "                        logR=FloatSlider(min=0., max=6., value=2., step=1., continuous_update=False, description = \"$log_{10}(R)$\"),\\\n",
    "                        logL=FloatSlider(min=-7., max=-2., value=-4., step=1., continuous_update=False, description = \"$log_{10}(L)$\"),\\\n",
    "                        logf=FloatSlider(min=0., max=8., value=5., step=1., continuous_update=False, description = \"$log_{10}(f)$\"))\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Supporting Derivation for the Frequency Response\n",
    "\n",
    "Consider a transmitter loop which carries a harmonic primary current $I_p(\\omega)$. According to the Biot-Savart law, this results in a primary magnetic field:\n",
    "\\begin{equation}\n",
    "\\mathbf{B_p} (\\mathbf{r},\\omega) = \\boldsymbol{\\beta} \\, I_p(\\omega)  \\;\\;\\;\\; \\textrm{where} \\;\\;\\;\\;\\; \\boldsymbol{\\beta} = \\frac{\\mu_0}{4 \\pi} \\int_C \\frac{d \\mathbf{l} \\times \\mathbf{r'}}{|\\mathbf{r'}|^2}\n",
    "\\end{equation}\n",
    "where $\\boldsymbol{\\beta}$ contains the problem geometry. Assume the magnetic field is homogeneous through the receiver loop. The primary field generates an EMF within the receiver loop equal to:\n",
    "\\begin{equation}\n",
    "EMF = - i\\omega \\Phi \\;\\;\\;\\;\\; \\textrm{where} \\;\\;\\;\\;\\; \\Phi =  A \\beta_n I_p(\\omega)\n",
    "\\end{equation}\n",
    "where $A$ is the area of the receiver loop and $\\beta_n$ is the component of $\\boldsymbol{\\beta}$ along $\\hat n$. The EMF induces a secondary current $I_s(\\omega)$ within the receiver loop. The secondary current is defined by the following expression:\n",
    "\\begin{equation}\n",
    "V = - i \\omega  A \\beta_n I_p (\\omega) =  \\big (R + i\\omega L \\big )I_s(\\omega)\n",
    "\\end{equation}\n",
    "Rearranging this expression to solve for the secondary current we obtain\n",
    "\\begin{equation}\n",
    "I_s (\\omega) = - \\frac{i \\omega  A \\beta_n}{R + i \\omega L} I_p(\\omega)\n",
    "\\end{equation}\n",
    "The secondary current has both real and imaginary components. These are given by:\n",
    "\\begin{equation}\n",
    "I_{Re} (\\omega) = - \\frac{i \\omega A \\beta_n R}{R^2 + (\\omega L)^2} I_p(\\omega)\n",
    "\\end{equation}\n",
    "and\n",
    "\\begin{equation}\n",
    "I_{Im} (\\omega) = - \\frac{ \\omega^2 A \\beta_n L}{R^2 + (\\omega L)^2} I_p(\\omega)\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Supporting Derivation for the Time-Harmonic Response\n",
    "\n",
    "Consider a transmitter loop which carries a harmonic primary current of the form:\n",
    "\\begin{equation}\n",
    "I_p(t) = I_0 \\textrm{cos} (\\omega t)\n",
    "\\end{equation}\n",
    "According to the Biot-Savart law, this results in a primary magnetic field:\n",
    "\\begin{equation}\n",
    "\\mathbf{B_p} (\\mathbf{r},t) =  \\boldsymbol{\\beta} \\, I_0 \\, \\textrm{cos} (\\omega t)  \\;\\;\\;\\; \\textrm{where} \\;\\;\\;\\;\\; \\boldsymbol{\\beta} = \\frac{\\mu_0}{4 \\pi} \\int_C \\frac{d \\mathbf{l} \\times \\mathbf{r'}}{|\\mathbf{r'}|^2}\n",
    "\\end{equation}\n",
    "where $\\boldsymbol{\\beta}$ contains the problem geometry. If the magnetic field is homogeneous through the receiver loop, the primary field generates an EMF within the receiver loop equal to:\n",
    "\\begin{equation}\n",
    "EMF = - \\frac{\\partial \\Phi}{\\partial t} \\;\\;\\;\\;\\; \\textrm{where} \\;\\;\\;\\;\\; \\Phi = A\\hat n \\cdot \\mathbf{B_p} = I_0 A \\beta_n \\, \\textrm{cos} (\\omega t)\n",
    "\\end{equation}\n",
    "where $A$ is the area of the receiver loop and $\\beta_n$ is the component of $\\boldsymbol{\\beta}$ along $\\hat n$. The EMF induces a secondary current $I_s$ within the receiver loop. The secondary current is defined by the following ODE:\n",
    "\\begin{equation}\n",
    "V = \\omega I_0 A \\beta_n \\, \\textrm{sin} (\\omega t) = I_s R + L \\frac{dI_s}{dt}\n",
    "\\end{equation}\n",
    "The ODE has a solution of the form:\n",
    "\\begin{equation}\n",
    "I_s (t) = \\alpha \\, \\textrm{cos} (\\omega t - \\phi)\n",
    "\\end{equation}\n",
    "where $\\alpha$ is the amplitude of the secondary current and $\\phi$ is the phase lag. By solving the ODE, the secondary current induced in the receiver loop is given by:\n",
    "\\begin{equation}\n",
    "I_s (t) = - \\Bigg [ \\frac{\\omega I_0 A \\beta_n}{R \\, \\textrm{sin} \\phi + \\omega L \\, \\textrm{cos} \\phi} \\Bigg ] \\, \\textrm{cos} (\\omega t -\\phi) \\;\\;\\;\\;\\; \\textrm{where} \\;\\;\\;\\;\\; \\phi = \\textrm{tan}^{-1} \\Bigg ( \\frac{\\omega L}{R} \\Bigg ) \\, \\in \\, [\\pi/2, \\pi ]\n",
    "\\end{equation}\n",
    "The secondary current has both in-phase and quadrature components, these are given by:\n",
    "\\begin{equation}\n",
    "\\textrm{In phase:} \\, I_s (t) = - \\Bigg [ \\frac{\\omega I_0 A \\beta_n}{R \\, \\textrm{sin} \\phi + \\omega L \\, \\textrm{cos} \\phi} \\Bigg ] \\textrm{cos} \\phi \\, \\textrm{cos} (\\omega t)\n",
    "\\end{equation}\n",
    "and\n",
    "\\begin{equation}\n",
    "\\textrm{Quadrature:} \\, I_s (t) = - \\Bigg [ \\frac{\\omega I_0 A \\beta_n}{R \\, \\textrm{sin} \\phi + \\omega L \\, \\textrm{cos} \\phi} \\Bigg ] \\textrm{sin} \\phi \\, \\textrm{sin} (\\omega t)\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "widgets": {
   "state": {
    "f0ce0f2e8021462c9d77a782d73389bb": {
     "views": [
      {
       "cell_index": 3
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
